#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.validator;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Description: 限制传入的参数在指定的值之中
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Documented
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ShutterIsland.Guarder.class)
public @interface ShutterIsland {

    String message() default "teddy weakup";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    String[] teddy() default {};

    class Guarder implements ConstraintValidator<ShutterIsland, Object> {

        String[] teddy;

        @Override
        public void initialize(ShutterIsland constraintAnnotation) {
            teddy = constraintAnnotation.teddy();
        }

        @Override
        public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {

            if (o == null || teddy == null) {
                return Boolean.TRUE;
            }

            String memory = String.valueOf(o);
            boolean weakup = Boolean.FALSE;

            for (int i = 0; i < teddy.length; i++) {
                if (teddy[i].equals(memory)) {
                    weakup = Boolean.TRUE;
                    break;
                }
            }

            return weakup;
        }
    }

}
